package com.dimples.dd.system.controller;

import com.dimples.dd.common.constant.DDServerConstant;
import com.dimples.dd.common.model.Option;
import com.dimples.dd.common.pojo.PageResult;
import com.dimples.dd.common.result.CommonResult;
import com.dimples.dd.log.core.annotation.ApiLog;
import com.dimples.dd.log.core.enums.OperateTypeEnum;
import com.dimples.dd.system.model.form.DataSourceConfigForm;
import com.dimples.dd.system.model.query.DataSourceConfigPageQuery;
import com.dimples.dd.system.model.vo.DataSourceConfigPageVO;
import com.dimples.dd.system.service.SystemDataSourceConfigService;
import com.dimples.dd.web.core.annotation.PreventDuplicateResubmit;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zhongyj <1126834403@qq.com><br/>
 * @date 2024/10/19
 */
@Tag(name = "数据源管理接口")
@RestController
@Validated
@RequestMapping("data-source-config")
@RequiredArgsConstructor
public class SysDataSourceConfigController {

    private final SystemDataSourceConfigService systemDataSourceConfigService;

    @Operation(summary = "数据源分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('system:data-source-config:query')")
    @ApiLog(operateType = OperateTypeEnum.SELECT, applicationName = DDServerConstant.DD_SERVER_SYSTEM)
    public PageResult<DataSourceConfigPageVO> selectDataSourceConfigPage(
            @ParameterObject DataSourceConfigPageQuery queryParams
    ) {
        return systemDataSourceConfigService.selectDataSourceConfigPage(queryParams);
    }

    @Operation(summary = "数据源下拉列表")
    @GetMapping("/options")
    @PreAuthorize("@ss.hasPerm('system:data-source-config:query')")
    public CommonResult<List<Option<String>>> listDataSourceConfigOptions() {
        List<Option<String>> list = systemDataSourceConfigService.listDataSourceConfigOptions();
        return CommonResult.success(list);
    }

    @Operation(summary = "数据源表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('system:data-source-config:query')")
    @ApiLog(operateType = OperateTypeEnum.SELECT, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<DataSourceConfigForm> selectDataSourceConfigForm(
            @Parameter(description = "数据源ID") @PathVariable Long id
    ) {
        DataSourceConfigForm dataSourceConfigForm = systemDataSourceConfigService.selectDataSourceConfigForm(id);
        return CommonResult.success(dataSourceConfigForm);
    }

    @Operation(summary = "新增数据源")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('system:data-source-config:create')")
    @PreventDuplicateResubmit
    @ApiLog(operateType = OperateTypeEnum.CREATE, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<Boolean> createDataSourceConfig(@Valid @RequestBody DataSourceConfigForm dataSourceConfigForm) {
        boolean result = systemDataSourceConfigService.createDataSourceConfig(dataSourceConfigForm);
        return CommonResult.success(result);
    }

    @Operation(summary = "修改数据源")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('system:data-source-config:update')")
    @ApiLog(operateType = OperateTypeEnum.UPDATE, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<Boolean> updateDataSourceConfig(
            @Parameter(description = "数据源ID") @PathVariable Long id,
            @Valid @RequestBody DataSourceConfigForm dataSourceConfigFormForm) {
        boolean result = systemDataSourceConfigService.updateDataSourceConfig(dataSourceConfigFormForm);
        return CommonResult.success(result);
    }

    @Operation(summary = "删除数据源")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('system:data-source-config:delete')")
    @ApiLog(operateType = OperateTypeEnum.DELETE, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<Boolean> deleteDataSourceConfigs(
            @Parameter(description = "删除数据源，多个以英文逗号(,)分割") @NotNull(message = "不能为空") @PathVariable String ids
    ) {
        boolean result = systemDataSourceConfigService.deleteDataSourceConfigs(ids);
        return CommonResult.success(result);
    }

    @Operation(summary = "测试数据源连接")
    @PostMapping("/test-connection")
    @PreAuthorize("@ss.hasPerm('system:data-source-config:create')")
    @ApiLog(operateType = OperateTypeEnum.OTHER, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<Boolean> testConnection(@Valid @RequestBody DataSourceConfigForm dataSourceConfigForm) {
        boolean result = systemDataSourceConfigService.testConnection(dataSourceConfigForm);
        return CommonResult.success(result);
    }
}
